package com.bjpowernode.controller;

import com.bjpowernode.service.DynamicService;
import com.bjpowernode.service.FollowService;
import com.bjpowernode.service.UserService;
import com.bjpowernode.user.Dynamic;
import com.bjpowernode.user.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "dynamic")
public class DynamicController{

    @Resource
    private DynamicService dynamicService;

    @Resource
    private UserService userService;

    @Resource
    private FollowService followService;

    @RequestMapping(value = "/selectSome.do")
    public ModelAndView selectSomeDynamic(@RequestParam("dynamic") String dynamic){
        List<Dynamic> dynamicList = dynamicService.findSomeDynamic(dynamic);
        ModelAndView mv = new ModelAndView();
        mv.addObject("dynamicList",dynamicList);
        mv.addObject("dynamic",dynamic);

        List<User> userList = userService.findSomeUser(dynamic);
        mv.addObject("userList",userList);
        mv.setViewName("search");
        return mv;
    }

    @RequestMapping(value = "/selectSomeTwo.do")
    public ModelAndView selectSomeDynamicTwo(@RequestParam("dynamic") String dynamic){
        List<Dynamic> dynamicList = dynamicService.findSomeDynamic(dynamic);
        ModelAndView mv = new ModelAndView();
        mv.addObject("dynamicListTwo",dynamicList);
        mv.addObject("dynamicTwo",dynamic);

        List<User> userList = userService.findSomeUser(dynamic);
        mv.addObject("userListTwo",userList);
        mv.setViewName("shouye");
        return mv;
    }

    @RequestMapping(value = "/selectAll.do")
    public ModelAndView selectAllDynamic(){
        ModelAndView mv = new ModelAndView();
        List<Dynamic> allDynamic = dynamicService.findAllDynamic();
        mv.addObject("dynamicList",allDynamic);
        mv.setViewName("findDynamic");
        return mv;
    }

    @RequestMapping(value = "/selectUser.do")
    public ModelAndView selectUserDynamic(@RequestParam("name") String name){
        ModelAndView mv = new ModelAndView();
        List<Dynamic> dynamicList = dynamicService.findUserDynamic(name);
        mv.addObject("dynamicList",dynamicList);
        mv.setViewName("userDynamic");
        return mv;
    }

    @RequestMapping(value = "/add.do",produces = "application/text;charset=utf-8")
    @ResponseBody
    public String addDynamic(@RequestParam("text") String text,@RequestParam("username") String username){
        Dynamic dynamic = new Dynamic();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(new Date());
        dynamic.setFbtime(time);
        dynamic.setDt(text);
        dynamic.setUsername(username);
        int result = dynamicService.addDynamic(dynamic);
        return result == 1 ? "发表成功" : "发表失败" ;
    }

    @RequestMapping(value = "/selectFollow.do")
    public ModelAndView selectFollow(@RequestParam("name") String name){
        ModelAndView mv = new ModelAndView();
        List<Dynamic> dynamicList = dynamicService.findFollowDynamic(name);
        mv.addObject("dynamicList",dynamicList);
        List<User> userList = followService.findSelfFollow(name);
        mv.addObject("userList",userList);
        mv.setViewName("followDynamic");
        return mv;
    }

    @RequestMapping(value = "/selectSelfDynamic.do")
    public ModelAndView selectSelfDynamic(@RequestParam("name") String name){
        ModelAndView mv = new ModelAndView();
        List<Dynamic> dynamicList = dynamicService.findSelfDynamic(name);
        mv.addObject("dynamicList",dynamicList);
        mv.setViewName("selfDynamic");
        return mv;
    }

    @RequestMapping(value = "/selectDynamicSum.do")
    @ResponseBody
    public Integer selectDynamicSum(@RequestParam("username") String username){
        return dynamicService.findDynamicSum(username);
    }
}
